package com.novax.ex.activity.provider.controller;

import com.novax.ex.activity.infrastructure.entity.OperationV2SubRule;
import com.novax.ex.activity.infrastructure.entity.result.OperationV2ConfigOuterDb;
import com.novax.ex.activity.infrastructure.entity.result.OperationV2SubConfigOuterDb;
import com.novax.ex.activity.open.api.OperationV2Api;
import com.novax.ex.activity.open.model.response.OperationV2ConfigOuterResponse;
import com.novax.ex.activity.open.model.response.OperationV2SubConfigOuterResponse;
import com.novax.ex.activity.provider.common.Operation;
import com.novax.ex.activity.provider.service.OperationV2ConfigService;
import com.novax.ex.activity.provider.service.OperationV2SubConfigService;
import com.novax.ex.activity.provider.service.OperationV2SubRuleService;
import com.novax.ex.common.enums.LanguageType;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Description 活动落地页配置
 * @Author ChenXi
 * @Date 2023/04/18/10:08
 */

@RestController
public class OperationV2Controller extends BaseActivityController implements OperationV2Api {

    @Resource
    private OperationV2ConfigService operationV2ConfigService;
    @Resource
    private OperationV2SubRuleService operationV2SubRuleService;

    @Override
    public ReturnResult<OperationV2ConfigOuterResponse> query(String language, String customId) {
        OperationV2ConfigOuterDb operationV2ConfigOuterDb = operationV2ConfigService.query(language, customId);

        // 非英语前提下
        if (!language.equals(LanguageType.en.getType())
            && Objects.nonNull(operationV2ConfigOuterDb)
            && StringUtils.isEmpty(operationV2ConfigOuterDb.getTitle())
            && StringUtils.isEmpty(operationV2ConfigOuterDb.getRule())) {
            operationV2ConfigOuterDb = operationV2ConfigService.query(LanguageType.en.getType(), customId);
        }
        return ReturnResult.success(CopyUtils.copyObject(operationV2ConfigOuterDb, OperationV2ConfigOuterResponse.class));
    }

    @Override
    public ReturnResult<List<OperationV2SubConfigOuterResponse>> openSubList(String language, Long operationId) {
        List<OperationV2SubConfigOuterDb> operationV2SubConfigOuterDbs = operationV2ConfigService.openSubList(language,
                                                                                                              operationId);

        // 非英语前提下
        if (!language.equals(LanguageType.en.getType())) {
            for (OperationV2SubConfigOuterDb operationV2SubConfigOuterDb : operationV2SubConfigOuterDbs) {
                if (Objects.nonNull(operationV2SubConfigOuterDb)
                    && StringUtils.isEmpty(operationV2SubConfigOuterDb.getTitle())
                    && StringUtils.isEmpty(operationV2SubConfigOuterDb.getRule())
                    && StringUtils.isEmpty(operationV2SubConfigOuterDb.getH5Img())
                    && StringUtils.isEmpty(operationV2SubConfigOuterDb.getPcImg())
                ) {
                    OperationV2SubRule enRule = operationV2SubRuleService.queryRule(
                        operationV2SubConfigOuterDb.getOperationSubId(), LanguageType.en.getType());
                    if (Objects.nonNull(enRule)) {
                        operationV2SubConfigOuterDb.setTitle(enRule.getTitle());
                        operationV2SubConfigOuterDb.setRule(enRule.getRule());
                        operationV2SubConfigOuterDb.setPcImg(enRule.getPcImg());
                        operationV2SubConfigOuterDb.setH5Img(enRule.getH5Img());
                    }
                }
            }
        }

        return ReturnResult.success(CopyUtils.copyList(operationV2SubConfigOuterDbs, OperationV2SubConfigOuterResponse.class));
    }

}
